package com.macro.mall.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.mapper.UmsMemberMapper;
import com.macro.mall.model.UmsMember;
import com.macro.mall.model.UmsMemberExample;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@ResponseBody
@Controller
@RequestMapping("/member")
@Api(tags = "UmsMemberController", description = "后台用户管理")
public class UmsMemberController {

    @Autowired
    private UmsMemberMapper memberMapper;

    @ApiOperation("获取用户所有用户")
    @RequestMapping(value = "/getMemberList")
    public CommonResult<PageInfo<UmsMember>> getMemberList(@RequestBody UmsMember member,
                                                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        UmsMemberExample umsMemberExample = new UmsMemberExample();
        UmsMemberExample.Criteria criteria = umsMemberExample.createCriteria();
        if (null != member.getId()) {
            criteria.andIdEqualTo(member.getId());
        }
        if (null != member.getNickname()) {
            criteria.andNicknameEqualTo(member.getNickname());
        }
        if (null != member.getPhone()) {
            criteria.andPhoneEqualTo(member.getPhone());
        }
        if (null != member.getStatus()) {
            criteria.andStatusEqualTo(member.getStatus());
        }
        PageHelper.startPage(pageNum, pageSize);
        List<UmsMember> umsMembers = memberMapper.selectByExample(umsMemberExample);
        PageInfo<UmsMember> pageInfo = new PageInfo<>(umsMembers);
        return CommonResult.success(pageInfo);
    }

    @ApiOperation("修改指定用户信息")
    @RequestMapping(value = "/updateMember")
    public CommonResult updateMember(@RequestBody UmsMember member) {
//        member.setStatus(0);
        memberMapper.updateByPrimaryKeySelective(member);
        return CommonResult.success(null);
    }
}
